$("document").ready(function(){

	$("#register").addClass("active");
	//用户名字为空
	$("#user_name").blur(function(){
		if ($("#user_name").val() === "") {
			$("#user_name").addClass("form-error");
			$("#name_result").html("用户姓名不能为空");
		} else {
			$("#user_name").removeClass("form-error");
			$("#user_name").addClass("form-success");
			$("#name_result").html("");
		}
		submitCondition();
	});

	//用户电话为空和ajax
	$("#user_phone").blur(function(){
		if ($("#user_phone").val() === "") {
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码不能为空");
		}else if (!checkPhone($("#user_phone").val())){
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码格式不正确");
		} else {
			$.ajax({
				type:'post',
				url:'/login/checkUser.action',
				dataType:'json',
				data:{
					user_phone:$("#user_phone").val(),
					user_type :'0'
				},
				success:function(data){
					if (data.status === "User_Not_Registered") {
						$("#user_phone").removeClass("form-error");
						$("#phone_result").html("");
					} else if (data.status === "User_Empty") {
						$("#user_phone").addClass("form-error");
						$("#phone_result").html(data.msg);
					}else if (data.status === "User_Exist"){
						$("#user_phone").addClass("form-error");
						$("#phone_result").html(data.msg);
					}else if (data.status === "Check_Fail"){
						sweetAlert("ERROR","服务异常","error");
					}else if (data.status === "Incorrect_Phone"){
						sweetAlert("WARNING","电话号码格式不正确","warning");
					}else {
						sweetAlert("发生错误","","error");
					}
				},
				error:function(jqXHR){
					//alert("发生错误" + jqXHR.status);
					sweetAlert("发生错误",jqXHR.status,"error");
				}
			});
		}
		submitCondition();
	});
	//email为空和ajax
	$("#user_email").blur(function(){
		if ($("#user_email").val() === "") {
			$("#user_email").addClass("form-error");
			$("#email_result").html("邮箱不能为空");
		}else if (!checkEmail($('#user_email').val())){
			$("#user_email").addClass("form-error");
			$("#email_result").html("邮箱格式不正确");
		} else {
			$("#user_email").removeClass("form-error");
			$("#user_email").addClass("form-success");
			$("#email_result").html("");
		}
		submitCondition();
	});
	//密码为空
	$("#user_pwd").blur(function(){
		if ($("#user_pwd").val() === "") {
			$("#user_pwd").addClass("form-error");
			$("#pwd_result").html("密码不能为空");
		}else if (!checkPassword($("#user_pwd").val())){
			$("#user_pwd").addClass("form-error");
			$("#pwd_result").html("密码需含有字母、数字以及密码长度在6-15位之间");
		}  else {
			$("#user_pwd").removeClass("form-error");
			$("#user_pwd").addClass("form-success");
			$("#pwd_result").html("");
		}
		submitCondition();
	});
	//确认密码为空或确认密码与密码不相同
	$("#pwd_confirm").blur(function(){
		if ($("#pwd_confirm").val() === "") {
			$("#pwd_confirm").addClass("form-error");
			$("#confirm_result").html("确认密码不能为空");
		//下面确认密码条件有误
		} else if ($("#user_pwd").val() !== $("#pwd_confirm").val()) {
			$("#pwd_confirm").addClass("form-error");
			$("#confirm_result").html("密码与确认密码不相同");
		} else {
			$("#pwd_confirm").removeClass("form-error");
			$("#pwd_confirm").addClass("form-success");
			$("#confirm_result").html("");
		};
		submitCondition();
	});
});
//表单存在没填选项或填写有错误，提交按钮为disabled
function submitCondition(){
	if ($("*").hasClass("form-error")) {
		$("#submit").addClass("disabled");
	} else {
		$("#submit").removeClass("disabled");
	}
}

function register() {
	console.error("here1");
	var user_name = $('#user_name').val();
	var user_phone = $('#user_phone').val();
	var user_email = $('#user_email').val();
	var user_pwd = $('#user_pwd').val();
	var pwd_confirm = $('#pwd_confirm').val();
	var user_type = 0;
	if(user_name == "" || user_phone == "" || user_email == "" || user_pwd == ""){
		sweetAlert("WARNING","请填写用户完整信息","warning");
		return false;
	}
	if (user_pwd != pwd_confirm){
		sweetAlert("WARNING","前后密码不一致","warning");
		return false;
	}
	console.error("here2");
	$.ajax({
		type:'post',
		url:'/register/register.action',
		dataType:'json',
		data:{
			user_phone:user_phone,
			user_name:user_name,
			user_email:user_email,
			user_password:user_pwd,
			password_confirm:pwd_confirm,
			user_type :user_type
		},
		timeout:60000,
		beforeSend:function () {
			$("#submit").empty();
			$("#submit").html("<img height='30' width='30' src='/image/loading.gif'/>");
			$("#submit").addClass("disabled");
		},
		success:function(data){
			if (data.status === "User_Empty") {
				$("#user_phone").removeClass("form-error");
				$("#phone_result").html("");
			} else if (data.status === "Incorrect_Phone_Format") {
				$("#user_phone").addClass("form-error");
				$("#phone_result").html(data.msg);
			}else if (data.status === "Incorrect_Email_Format"){
				$("#user_email").addClass("form-error");
				$("#email_result").html(data.msg);
			}else if (data.status === "Different_PWD"){
				$('#pwd_confirm').addClass("form-error");
				$('#confirm_result').html(data.msg);
			}else if (data.status === "Incorrect_PWD_Format"){
				$('#user_pwd').addClass("form-error");
				$('#pwd_result').html(data.msg);
			}else if (data.status === "Register_Repeated"){
				sweetAlert("WARNING","该号码已经注册","warning");
			}else if (data.status === "Register_Success"){
				swal({
					title: "Success",
					text: "注册成功",
					type: "success",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.href = data.url;
				});
				setTimeout(function(){
					window.location.href = data.url;
				},3000);
			}else if (data.status === "Register_Fail"){
				swal({
					title: "ERROR",
					text: "注册失败",
					type: "error",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.reload();
				});
				setTimeout(function(){
					window.location.reload();
				},3000);
			}else if (data.status === "Check_Fail"){
				swal({
					title: "ERROR",
					text: "发生错误",
					type: "error",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.reload();
				});
				setTimeout(function(){
					window.location.reload();
				},3000);
			}else if (data.status === "Image_Needed"){
				sweetAlert("WARN","商家需要上传营业执照","warning");
			}else {
				swal({
					title: "ERROR",
					text: "发生错误",
					type: "error",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.reload();
				});
				setTimeout(function(){
					window.location.reload();
				},3000);
			}
		},
		complete:function () {
			$("#submit").empty();
			$("#submit").html("注册");
			$("#submit").removeClass("disabled");
		},
		error:function(jqXHR){
			//alert("发生错误" + jqXHR.status);
			swal({
				title: jqXHR.status,
				text: "发生错误",
				type: "error",
				showCancelButton: false,
				closeOnConfirm: true,
				confirmButtonText: "确定"
			}, function() {
				window.location.reload();
			});
			setTimeout(function(){
				window.location.reload();
			},3000);

		}
	});
	submitCondition();
	return false;
}

function checkPhone(phone) {
	var rex = /^1[3|4|5|8][0-9]\d{4,8}$/
	if (rex.test(phone)){
		return true;
	}else {
		return false;
	}
}
function checkEmail(email) {
	var rex = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
	if (rex.test(email))
		return true;
	else
		return false;
}
function checkPassword(password) {
	var  rex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/
	if (rex.test(password)){
		return true;
	}else {
		return false;
	}
}
